<template>
    <div>
        <headerX></headerX>
        <menuX :menus="menuList"></menuX>
        <contentX></contentX>
    </div>
</template>
<script>
import headerX from '../components/layout/Header.vue'
import menuX from '../components/layout/Menu.vue'
import contentX from '../components/layout/Content.vue'

export default {
    name: 'Index',
    data () {
        return {
            menuList: [
                {id: 1, name: '正在加载...', url: '/', parentId: 0, isMenu: 1, icon: 'fa fa-gear'},
                {id: 2, name: '请稍等...', url: '/', parentId: 0, isMenu: 1, icon: 'fa fa-gear'}
            ]
        }
    },
    components: {
        // 其他组件在此引用，之前要import
        headerX: headerX, menuX: menuX, contentX: contentX
    },
    created () {
        // 判断用户有无登录
        let token = $.cookie('token')
        let userInfoStr = $.cookie('userInfo')
        if (!token || !userInfoStr) {
            // 用户没登录
            // this.$router.push('/login')
            top.location.replace('#/login')
        } else {
            this.$request.get(this.$apiSys.loginMenusUrl, {}, true).then(res => {
                if (res.rows !== undefined && Array.isArray(res.rows) && res.rows.length > 0) {
                    // this.$ls.setItem('userMenus', JSON.stringify(res.rows))
                    let userMenuUrls = []
                    for (let i = 0; i < res.rows.length; i++) {
                        if (res.rows[i].url) {
                            userMenuUrls.push(res.rows[i].url)
                        }
                    }
                    $.cookie('userMenuUrls', JSON.stringify(userMenuUrls))
                    this.menuList = res.rows
                } else {
                    this.menuList = [{
                        id: 1,
                        name: '没有权限',
                        url: '/',
                        parentId: 0,
                        isMenu: 1,
                        icon: 'fa fa-gear'
                    }, {id: 2, name: '请联系管理员解决', url: '/', parentId: 0, isMenu: 1, icon: 'fa fa-gear'}]
                }
            }, error => {
                console.log(error)
            })
        }
    }
}
</script>

<style>
</style>
